import pygame.font
from ship import Ship
from pygame.sprite import Group
class ScoreBoard():
    """记分牌"""
    def __init__(self, ai_settings, screen, stats):
        self.screen = screen
        self.screen_rect = self.screen.get_rect()
        self.settings = ai_settings
        self.stats = stats


        # 设置记分牌的字体和颜色
        self.text_color = (30, 30, 30)
        self.font = pygame.font.SysFont(None, 48, True, True)

        # 渲染计分板的图像
        self.prep_score()
        self.prep_high_score()
        self.prep_level()
        self.prep_ship()

    def prep_score(self):
        """将得分数据渲染为图像"""
        # 将得分设置为10的倍数并按照千分位使用逗号分隔
        round_score = int(round(self.stats.score, -1))
        score_str = "{:,}".format(round_score)
        # 根据得分字符串创建得分图像并设置位置为屏幕的右上角
        self.score_image = self.font.render(score_str, True, self.text_color, self.settings.screen_bg_color)
        self.score_image_rect = self.score_image.get_rect()
        self.score_image_rect.right = self.screen_rect.right - 20
        self.score_image_rect.top = self.screen_rect.top

    def prep_high_score(self):
        """将最好得分渲染为图像并设置其显示的位置"""
        high_round_score = int(round(self.stats.high_score, -1))
        high_score_str = "{:,}".format(high_round_score)
        # 根据最高等分创建图像并设置图像的位置
        self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.settings.screen_bg_color)
        self.high_score_image_rect = self.high_score_image.get_rect()
        self.high_score_image_rect.center = self.screen_rect.center
        self.high_score_image_rect.top = self.screen_rect.top

    def prep_level(self):
        """将等级渲染为图片并设置其显示位置"""
        level_str = str(self.stats.level)
        self.level_image = self.font.render(level_str, True, self.text_color, self.settings.screen_bg_color)
        self.level_image_rect = self.level_image.get_rect()
        self.level_image_rect.right = self.screen_rect.right - 20
        self.level_image_rect.top = self.score_image_rect.top + 30

    def prep_ship(self):
        """根据剩余的飞船数量创建一个飞船对象的编组"""
        self.ships = Group()
        for ship_number in range(self.stats.ship_left):
            ship = Ship(self.settings, self.screen)
            ship.rect.x = 10 + ship_number*ship.rect.width
            ship.rect.y = 10
            self.ships.add(ship)

    def show_score(self):
        """在屏幕上显示计分板"""
        self.screen.blit(self.score_image, self.score_image_rect)
        self.screen.blit(self.high_score_image, self.high_score_image_rect)
        self.screen.blit(self.level_image, self.level_image_rect)
        self.ships.draw(self.screen)
